package com.cff.mina.skt;

import java.net.InetSocketAddress;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class MinaUtil {
	private String ip;
	private int port;
	private byte[] msg;
	
	public MinaUtil(String ip, int port,byte[] msg){
		this.ip = ip;
		this.port = port;
		this.msg = msg;
		System.out.println(ip+"----"+port);
		NioSocketConnector connector = new NioSocketConnector() ; 
		connector.setConnectTimeoutMillis(30000); //设置连接超时  
        connector.getSessionConfig().setReceiveBufferSize(10240);   // 设置接收缓冲区的大小  
        connector.getSessionConfig().setSendBufferSize(10240);// 设置输出缓冲区的大小 
        //加入编码/解码Filter  
        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MsgCodecFactory())) ;  
          
        //设置IO处理器  
        connector.setHandler(new MinaClient(msg)) ;  
          
        //链接服务端  
        ConnectFuture connectFuture = connector.connect(new InetSocketAddress(ip, port)) ;  
      
          
//        //阻塞等待，知道链接服务器成功，或被中断  
//        connectFuture.awaitUninterruptibly() ;  
//        connectFuture.getSession().getCloseFuture().awaitUninterruptibly();     
//        
//        connector.dispose(); 
	}
}
